/**
 * 
 */
package com.demo2do.boyle.backend.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.demo2do.boyle.backend.cache.accessor.PersistentAccessor;
import com.demo2do.boyle.common.entity.aduser.Aduser;
import com.demo2do.boyle.common.entity.aduser.AduserLevel;
import com.demo2do.core.persistence.GenericDaoSupport;

/**
 * @author downpour leo
 *
 */
@Repository("aduserDaoSupport")
public class AduserDaoSupport {

	@Autowired
	private GenericDaoSupport genericDaoSupport;
	
	@Autowired
	private PersistentAccessor persistentAccessor;
	
	/**
	 * List aduser by mobile
	 * 
	 * @param mobile
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Aduser> listAduser(String mobile) {
		return genericDaoSupport.searchForList("FROM Aduser aduser WHERE aduser.disabled = false AND aduser.mobile = :mobile", "mobile", mobile);
	}
	
	/**
	 * Get default aduser level
	 * 
	 * @return
	 */
	public AduserLevel getDefaultAduserLevel() {
		List<AduserLevel> aduserLevels = persistentAccessor.getAduserLevels() ;
		for(AduserLevel aduserLevel : aduserLevels) {
			if(!aduserLevel.isDisabled() && aduserLevel.isPrimary()) {
				return aduserLevel;
			}	
		}
		return null;
	}
	
}
